package com.tanhua.server.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.pojo.Question;
import com.tanhua.common.pojo.UserThreadLocal;
import com.tanhua.dubbo.server.api.HuanXinApi;
import com.tanhua.dubbo.server.api.VisitorsApi;
import com.tanhua.dubbo.server.enums.HuanXinMessageType;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.TodayBest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TodayBestService {
    @Autowired
    UserService userService;
    @Autowired
    RecommendUserService recommendUserService;
    @Autowired
    UserInfoService userInfoService;
    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;

    public TodayBest queryTodayBest(String token) {
        User user = userService.queryUserByToken(token);
        if (user == null) {
            return null;
        }
        //使用dubbo服务
        TodayBest todayBest = recommendUserService.queryTodayBest(user.getId());
        if (todayBest == null) {
            todayBest = new TodayBest();
            todayBest.setId(defaultUser);
            todayBest.setFateValue(80L);
        }

        UserInfo userInfo = userInfoService.queryUserInfoByUserId(todayBest.getId());
        if(userInfo==null){
            return null;
        }
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setTags(StringUtils.split(userInfo.getTags(), ','));
        //todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
        todayBest.setGender(userInfo.getSex().name().toLowerCase());
        todayBest.setAge(userInfo.getAge());
        return todayBest;
    };

    public PageResult queryRecommendation(String token, RecommendUserQueryParam queryParam) {
        User user = userService.queryUserByToken(token);
        if (user == null) {
            return null;
        }

        PageResult pageResult = new PageResult();
        PageInfo<RecommendUser> recommendUserPageInfo=recommendUserService.queryRecommendUserList(user.getId(),queryParam.getPage(),queryParam.getPagesize());
        List<RecommendUser> records = recommendUserPageInfo.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return pageResult;
        }
        HashSet<Long> userIds = new HashSet<>();
        for (RecommendUser record : records) {
            userIds.add(record.getUserId());
        }
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIds);
//        if (StringUtils.isNotEmpty(queryParam.getGender())) {
//            //需要性别参数查询
//            queryWrapper.eq("sex", StringUtils.equals(queryParam.getGender(), "man") ? 1 : 2);
//        }
//        if (StringUtils.isNotEmpty(queryParam.getCity())) {
//            //需要城市参数查询
//            queryWrapper.like("city", queryParam.getCity());
//        }
//
//        if (queryParam.getAge() != null) {
//            //设置年龄参数，条件：小于等于
//            queryWrapper.le("age", queryParam.getAge());
//        }

        List<UserInfo> userInfoList = userInfoService.queryUserInfoList(queryWrapper);
        if(CollectionUtils.isEmpty(userInfoList)){
            //没有查询到用户的基本信息
            return pageResult;
        }
        ArrayList<TodayBest> todayBests = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            TodayBest todayBest = new TodayBest();
            todayBest.setId(userInfo.getUserId());
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setNickname(userInfo.getNickName());
            todayBest.setTags(StringUtils.split(userInfo.getTags(), ','));
            //todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
            todayBest.setGender(userInfo.getSex().name().toLowerCase());
            todayBest.setAge(userInfo.getAge());
            for (RecommendUser record : records) {
                if(record.getUserId()==userInfo.getUserId()){
                    Double score = record.getScore();
                    double fateValue = Math.ceil(score);
                    todayBest.setFateValue(Double.valueOf(score).longValue());
                    break;
                }
            }
            todayBests.add(todayBest);
        }
        Collections.sort(todayBests, new Comparator<TodayBest>() {
            @Override
            public int compare(TodayBest o1, TodayBest o2) {
                return new Long(o2.getFateValue()-o1.getFateValue()).intValue();
            }
        });
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());
        pageResult.setItems(todayBests);
        return pageResult;
    }


}
